User
<script setup>
import { Delete, Edit } from '@element-plus/icons-vue'

import { ref } from 'vue'
import { classesAddService, classesListService } from '@/api/classes.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { classesDeleteService, classesUpdateService } from '@/api/classes'
import { teachersListService } from '@/api/teacher.js'

const classes = ref([
  {
    id: '',
    name: '',
    grade: '',
    advisor: '',
    advisorName: '',
  }
])

const classId = ref('')
const id = ref('')
const grade = ref('')
const advisor = ref('')
const pageNum = ref(1)//当前页
const total = ref(0)//总条数
const pageSize = ref(5)//每页条数

const onSizeChange = (size) => {
  pageSize.value = size
  classesList()
}

const onCurrentChange = (num) => {
  pageNum.value = num
  classesList()
}

let pageSizes = [5, 10, 15];

const classesList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    classId: classId.value ? classId.value : null,
    id: id.value ? id.value : null,
    advisor: advisor.value ? advisor.value : null,
    grade: grade.value ? grade.value : null
  }

  let result = await classesListService(params);

  total.value = result.data.total;
  classes.value = result.data.items;
  for (let i = 0; i < classes.value.length; i++) {
    let class1 = classes.value[i];
    for (let j = 0; j < teachers.value.length; j++) {
      if (class1.advisor == teachers.value[j].id) {
        class1.advisorName = teachers.value[j].name;
      }
    }
  }
}

const teachers = ref();

const getteachers = async () => {
  let result = await teachersListService();
  teachers.value = result.data;
}

getteachers()

classesList()

const classModel = ref({
  id: '',
  name: '',
  grade: '',
  advisor: ''
})

const addClass = async () => {

  await classesAddService(classModel.value);
  ElMessage.success('添加成功');

  dialogVisible.value = false;

  await classesList()
}

const updateClass = async () => {

  try {
    await classesUpdateService(classModel.value);
    ElMessage.success('修改成功');
  } catch (error) {
    ElMessage.error('修改失败：' + error.message);
  }

  dialogVisible.value = false;

  await classesList();
}

const deleteClass = (row) => {

  ElMessageBox.confirm(
    '你确认要删除该班级吗?',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      let result = await classesDeleteService(row.id);
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      await classesList()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除',
      })
    })
}

const clearData = () => {
  classModel.value.id = '';
  classModel.value.name = '';
  classModel.value.grade = '';
  classModel.value.advisor = '';
}

const dialogVisible = ref(false)
const title = ref('')
const showDialog = (row) => {
  dialogVisible.value = true;
  title.value = '编辑班级'
  classModel.value.name = row.name;
  classModel.value.grade = row.grade;
  classModel.value.advisor = row.advisor;
  classModel.value.id = row.id
}

</script>
<template>
  <el-card>
    <template #header>
      <div class="header">
        <div class="extra">
          <el-button type="primary" @click="dialogVisible = true; clearData(); title = '添加班级'">添加班级</el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="班级ID：">
        <el-input v-model="id" placeholder="请输入班级ID"></el-input>
      </el-form-item>

      <el-form-item label="年级：">
        <el-input v-model="grade" placeholder="请输入班级年级"></el-input>
      </el-form-item>

      <el-form-item label="导师名字：">
        <el-input v-model="advisor" placeholder="请输入导师ID"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="classesList()">搜索</el-button>
        <el-button @click="id = ''; grade = ''; advisor = ''; classesList()">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 班级列表 -->
    <el-table :data="classes" style="width: 100%">
      <el-table-column label="班级号" prop="id"></el-table-column>
      <el-table-column label="班级" prop="name"></el-table-column>
      <el-table-column label="年级" prop="grade"></el-table-column>
      <el-table-column label="导师" prop="advisorName"></el-table-column>
      <el-table-column label="导师ID" prop="advisor"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteClass(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="pageSizes" :total="total"
      background layout="jumper, total, sizes, prev, pager, next" style="margin-top: 20px; justify-content: flex-end"
      @size-change="onSizeChange" @current-change="onCurrentChange" />

    <!-- 添加班级弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="classModel" label-width="100px" style="padding-right: 30px">
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="classModel.name"></el-input>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="classModel.grade"></el-input>
        </el-form-item>
        <el-form-item label="班级导师" prop="advisor">
          <el-select v-model="classModel.advisor" placeholder="请选择">
            <el-option v-for="t in teachers" :key="t.id" :label="t.name" :value="t.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="title == '添加班级' ? addClass() : updateClass()"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>